package com.ugrow.ugrowcommunity.activity.dormitoryselection;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.ugrow.ugrowcommunity.activity.dormitoryselection.adapter.DormitoryAdapter;
import com.ugrow.ugrowcommunity.activity.dormitoryselection.bean.DormitoryBean;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.ActivityDormitorySelectionBinding;
import com.ugrow.ugrowcommunity.util.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;


public class DormitorySelectionActivity extends AppCompatActivity {


    private ActivityDormitorySelectionBinding binding;
    private DormitoryAdapter dormitoryAdapter;
    private ArrayList<DormitoryBean.Data> arrayList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDormitorySelectionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
        initAdapter();
        initData();

    }



    //视图设置
    private void initView(){
        binding.hello.setText("欢迎您"+Data.StudentBean.getData().get(0).getStuName()+"同学。您还未选择宿舍，请完成宿舍预约。");
        binding.ACback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    //初始化适配器所需的数据
    private void initData(){
        HashMap hashMap = new HashMap();
        hashMap.put("sex",Data.StudentBean.getData().get(0).getStuSex());
        Log.d("TAG", "success: "+Data.StudentBean.getData().get(0).getStuSex());
        HttpUtil.httpPost("http://82.157.173.219:8080/SmartCampus/dor/selectAllBySex", hashMap, HttpUtil.ContentType.FROM, new HttpUtil.CallBack() {
            @Override
            public void success(HttpUtil.Result result) {

                String content = result.content;
                Log.d("TAG", "success: "+content);
                Gson gson = new Gson();
                DormitoryBean dormitoryBean = gson.fromJson(content, DormitoryBean.class);
                arrayList = dormitoryBean.getData();
                changeAdapter(arrayList);
            }

            @Override
            public void failed(IOException e) {
                Log.d("TAG", "查询宿舍信息失败");
            }
        });



    }

    //初始化适配器
    private void initAdapter(){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(DormitorySelectionActivity.this,3);
        binding.recDs.setLayoutManager(gridLayoutManager);
        dormitoryAdapter = new DormitoryAdapter();
        dormitoryAdapter.setContext(DormitorySelectionActivity.this);
        dormitoryAdapter.setArrayList(arrayList);
        binding.recDs.setAdapter(dormitoryAdapter);

    }

    //刷新适配器
    private void changeAdapter(ArrayList<DormitoryBean.Data> data) {
        dormitoryAdapter.setArrayList(data);
        //刷新适配器
        dormitoryAdapter.notifyDataSetChanged();
    }





}